package com.lz.comm

import android.app.Activity
import android.content.res.Configuration
import com.lz.library.base.ApplicationInitModule
import com.lz.library.base.BaseApp
import com.lz.library.extension.APP
import com.lz.library.extension.getScreenPoint
import com.tencent.mmkv.MMKV
import me.jessyan.autosize.AutoSize
import me.jessyan.autosize.AutoSizeConfig
import me.jessyan.autosize.onAdaptListener

/**
 *  author: you : 2020/10/31
 */
class CommAppInitModule : ApplicationInitModule() {

    private val screenPoint by lazy { APP.getScreenPoint() }

    override fun onCreate(app: BaseApp) {
        MMKV.initialize(app)
        AutoSize.initCompatMultiProcess(app)

        AutoSizeConfig.getInstance().onAdaptListener = object : onAdaptListener {
            override fun onAdaptBefore(target: Any, activity: Activity) {
                if (activity.resources.configuration.orientation == Configuration.ORIENTATION_LANDSCAPE) {
                    //设置横屏基准
                    AutoSizeConfig.getInstance().designWidthInDp = 640
                    AutoSizeConfig.getInstance().screenWidth = screenPoint.y
                    AutoSizeConfig.getInstance().screenHeight = screenPoint.x
                } else {
                    //设置竖屏基准
                    AutoSizeConfig.getInstance().designWidthInDp = 360
                    AutoSizeConfig.getInstance().screenWidth = screenPoint.x
                    AutoSizeConfig.getInstance().screenHeight = screenPoint.y
                }
            }

            override fun onAdaptAfter(target: Any, activity: Activity) = Unit
        }
    }
}